﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Extensions
{
	using System;
	using System.Collections.Generic;
	using System.Web.Mvc;
	using Falcon.Core.Collections;
	using Falcon.Core.Extensions;
	using Falcon.Forums.Domain.Enums;

	public static class ForumTopicExtension
	{
		public const string ForumTopicNew = "forum_topic_new";
		public const string ForumTopicOld = "forum_topic_old";
		public const string ForumTopicHotNew = "forum_topic_hot_new";
		public const string ForumTopicHotOld = "forum_topic_hot_old";
		public const string ForumTopicClosedNew = "forum_topic_closed_new";
		public const string ForumTopicClosedOld = "forum_topic_closed_old";

		/// <summary>
		/// Initializes the <see cref="ForumTopicExtension"/> class.
		/// </summary>
		static ForumTopicExtension()
		{
			Localized = new Dictionary<string, string>
			{
				{ ForumTopicNew, ForumsResources.Titles.NewPosts },
				{ ForumTopicOld, ForumsResources.Titles.NoNewPosts },
				{ ForumTopicHotNew, ForumsResources.Titles.HotNewPosts },
				{ ForumTopicHotOld, ForumsResources.Titles.HotNoNewPosts },
				{ ForumTopicClosedNew, ForumsResources.Titles.ClosedNewPosts },
				{ ForumTopicClosedOld, ForumsResources.Titles.ClosedNoNewPosts }
			};
		}

		/// <summary>
		/// Gets or sets the localized.
		/// </summary>
		/// <value>The localized.</value>
		private static Dictionary<string, string> Localized
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the status image icon for the topic.
		/// </summary>
		/// <param name="topic">The topic.</param>
		/// <param name="topics">The topics.</param>
		/// <returns></returns>
		public static string StatusImage(this ForumTopic topic, Dictionary<int, DateTime> topics)
		{
			var read = DateTime.Now.Subtract(topic.Updated).Days >= ForumsSettings.Topics.DaysReadExpire;

			if (topics != null)
			{
				read = topics.ContainsKey(topic.Id) && (topics[topic.Id] >= topic.Updated);
			}

			string key;

			if (topic.Status == ForumTopicStatus.Closed)
			{
				key = read ? ForumTopicClosedOld : ForumTopicClosedNew;
			}
			else if (topic.Replies > 20 && topic.Updated.Subtract(DateTime.Now).TotalDays < 5.0)
			{
				key = read ? ForumTopicHotOld : ForumTopicHotNew;
			}
			else
			{
				key = read ? ForumTopicOld : ForumTopicNew;
			}

			var img = new TagBuilder("img");

			img.MergeAttribute("src", string.Concat(ForumsSettings.Topics.IconPath.AsRoot(), "/", key, ".gif"));
			img.MergeAttribute("alt", Localized[key]);

			return img.ToString(TagRenderMode.SelfClosing);
		}

		/// <summary>
		/// Gets the forum topic actions.
		/// </summary>
		/// <param name="topics">The topics.</param>
		/// <returns></returns>
		public static SelectList Actions(this IPagedList<ForumTopic, ForumTopicSpecification> topics)
		{
			return new SelectList(Enum.GetNames(typeof(ForumTopicAction)), ForumTopicAction.Delete.Name());
		}
	}
}